/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package item;

import java.sql.Date;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import myJPA.Model;
import java.util.List;
import transactions.Rental;
import transactions.Reservation;
/**
 *
 * @author ZIZI
 */
public class ItemList {
    
    
    public static boolean addItem(Item item)
    {
        if(item!= null)
        {
            item.save();
            return true;
        }
        return false;
    }
    
  
    
    public static boolean removeItem(Item item)
    {
        item.remove();
        return true;
    }
    
    
    

    
   public static boolean addCopy(Item item)
    {
   
        Item ite= Model.getEntityManager().find(Item.class, item.getId());
        int i= ite.getNumberCopies();
        ite.setNumberCopies(++i);  
        ite.update();
         
        return true;
  
        
      
    }
    
    
    
   public static boolean RemoveCopy(Item it)
    {
       
        Item item= Model.getEntityManager().find(Item.class, it.getId());
        int i= item.getNumberCopies();
        item.setNumberCopies(--i);  
        item.save();
         
        return true;
  
        }
   
   
   public static String ReportItem(Item item)
    {   int i;
    
       String result; 
     
       result= "Title Item :"+item.getTitle();
       result += "\n Number of available copies is :  "+ item.getAvailableCopies();
       List<Rental> ls = item.getRentals();
       result += "\n list of customers that rent this title is:";
       for(i=0; i< ls.size();i++){
          if (ls.get(i).isReturned() == false ){
              
              result = result + "\n " + ls.get(i).getCustomer().getFullName()+"       " +ls.get(i).getDateReturn() ;
           
          }
       }
       
       result = result + "\n" + "List of customers that hold this title is:  " ;
       
       List<Reservation>  listRes = item.getReservations();
       
       if (listRes.isEmpty() == false ){
       
       for(i=0; i< listRes.size();i++)
          {
              if (listRes.get(i).isOnHold() == true)
              
              result = result + "\n" + listRes.get(i).getCustomer().getFullName() ;
           
             } 
       }  else  {
           
           result = result + " " + " Title Not on hold";
       
         }
       
    
       return result;
       
    }
   
   
    public static boolean SetRentalRate(int rate, String type) {
       
        EntityManager manager = Model.getEntityManager();
        Query q = manager.createNamedQuery("findCharge");
        q.setParameter("type", type);
        ItemInfo t = (ItemInfo) q.getSingleResult();
        t.setRentalCharge(rate);
        t.update();
        return true;
       
    }
   
    
      public static boolean SetRentalPeriod(int period, String type) {
       
        EntityManager manager = Model.getEntityManager();
        Query q = manager.createNamedQuery("findCharge");
        q.setParameter("type", type);
        ItemInfo t = (ItemInfo) q.getSingleResult();
        t.setRentalPeriod(period);
        t.update();
        return true;
       
    }
   
      
      
      public static String ReportOverdue(){  
          
        String result = "";
        java.sql.Date today = new Date(java.util.Calendar.getInstance().getTimeInMillis());
        EntityManager manager = Model.getEntityManager();
        Query q = manager.createNamedQuery("findRental");
        q.setParameter("today", today);
        List <Rental> rental = q.getResultList();
        
       for(int i=0; i< rental.size(); i++) {
           
           
           result += rental.get(i).getItem().getTitle()+ "       "+ rental.get(i).getDateReturn()+"\n";
           
       }
       
           return result;
           
      }
          
      
      
      
      
      
      
   
}
